package com.hnkywl.sts.action.cwgl.bzj;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.dao.zjgl.PjdrDao;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.cwgl.BzjdrService;
import com.hnkywl.sts.service.cwgl.TxcdService;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.zjgl.PjglService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-30
 * Time: 上午9:57
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/bzjdr")
public class BzjdrAction extends SimpleActionSupport {
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private Integer sfklx = 1;
    private String zt;
    private String bm;
    private Date sfkrqStart;
    private Date sfkrqEnd;
    private String sbgs;
    private Long sbgsId;
    private String gldwMc;
    private String bzjdrpjlxMc;
    private Integer bzjdrdrZt;
    private int page;
    private int rows;
    private BzjdrService bzjdrService;
    private Yg yg;
    private YgService ygService;
    private String gsmc;
    private Long bzjdrId;
    private String systemDate;
    private Map<String, Object> voucherMaster = new HashMap<String, Object>();
    //附单据数
    private Integer attachment;
    private VoucherService voucherService;
    private Map<String, Object> voucher;
    private Date bzjdrSystemDate;
    private Map<String, String> result = new HashMap<String, String>();
    private XmlParse xmlParse;
    private Gson gson = new Gson();
    private Date zzrq;
    private Sfk sfk;

    @Action("bzjdr")
    public String bzjdr() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //保证金分页显示
    @Action("bzjdrList")
    public void bzjdrList() {
        this.renderJson(this.bzjdrService.findAllBzjdr(sfklx, zt, bm, sfkrqStart, sfkrqEnd, sbgs, sbgsId, gldwMc, bzjdrpjlxMc, bzjdrdrZt, page, rows));
    }

    // 生成凭证页面
    @Action("bzjdr_content")
    public String bzjdrContent() {
        yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
        gsmc = this.bzjdrService.findBzjdrDetail(bzjdrId).getSbgs().getQc();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate = fmt.format(new Date());
        sfk = this.bzjdrService.findBzjdrDetail(bzjdrId);
        voucherMaster = this.bzjdrService.getVoucherMaster(bzjdrId, attachment, null, null, null, yg);
        return SUCCESS;
    }

    //查询财务收付分录
    @Action("findbzjdrfl")
    public void findbzjdrfl() {
        renderText(bzjdrService.findBzjdrFl(bzjdrId));
    }

    //财务收付导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有U8用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有U8密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
            Boolean begin = voucherService.beginGenerateVoucher(bzjdrId, VoucherType.BZJ);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            voucher = this.bzjdrService.getU8VoucherItems(bzjdrId, uuid, null, bzjdrSystemDate);
            //凭证头
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
//            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    this.bzjdrService.getVoucherMaster(bzjdrId, null, uuid, null, null, yg);
            //将头部可以修改的部分的修改值重新赋值
            voucherMaster.put("createdate", simpleDateFormat.format(bzjdrSystemDate));
            voucherMaster.put("acctyear", simpleDateFormat2.format(bzjdrSystemDate));
            voucherMaster.put("acctperiod", simpleDateFormat3.format(bzjdrSystemDate));
            voucherMaster.put("attachment", attachment);
            voucherMaster.put("memo", VoucherType.BZJ.getTag() + bzjdrId);//备注，在凭证删除时使用
            this.bzjdrService.bzjdrChangeDrrq(bzjdrId, bzjdrSystemDate);
            voucher.put("master", voucherMaster);
//            String r = voucherService.generateVoucher(voucher);
//            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
//                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, bzjdrId, VoucherType.BZJ);
//                if (!success)
//                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
//            voucherService.checkJiuqiUsernamePassword();
//            Boolean begin = voucherService.beginRevokeVoucher(bzjdrId, VoucherType.BZJ);
//            if (!begin)
//                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
//            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = this.bzjdrService.getRevokeInfo(bzjdrId);
            Map<String, Object> info = new HashMap<String, Object>();
//            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            revokeInfo.put("memo", VoucherType.BZJ.getTag() + bzjdrId);//备注，在凭证删除时使用
//            String r = voucherService.revokeVoucher(info);
//            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
//                Boolean success = voucherService.revokeVoucherSuccess(bzjdrId, VoucherType.BZJ);
//                if (!success)
//                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {

//        renderJson(voucherService.restVoucher(bzjdrId, VoucherType.BZJ));
    }


    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setBzjdrService(BzjdrService bzjdrService) {
        this.bzjdrService = bzjdrService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
//    @Autowired
//    public void setVoucherService(VoucherService voucherService) {
//        this.voucherService = voucherService;
//    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Integer getSfklx() {
        return sfklx;
    }

    public void setSfklx(Integer sfklx) {
        this.sfklx = sfklx;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getBm() {
        return bm;
    }

    public void setBm(String bm) {
        this.bm = bm;
    }

    public Date getSfkrqStart() {
        return sfkrqStart;
    }

    public void setSfkrqStart(Date sfkrqStart) {
        this.sfkrqStart = sfkrqStart;
    }

    public Date getSfkrqEnd() {
        return sfkrqEnd;
    }

    public void setSfkrqEnd(Date sfkrqEnd) {
        this.sfkrqEnd = sfkrqEnd;
    }

    public String getSbgs() {
        return sbgs;
    }

    public void setSbgs(String sbgs) {
        this.sbgs = sbgs;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public String getGldwMc() {
        return gldwMc;
    }

    public void setGldwMc(String gldwMc) {
        this.gldwMc = gldwMc;
    }

    public String getBzjdrpjlxMc() {
        return bzjdrpjlxMc;
    }

    public void setBzjdrpjlxMc(String bzjdrpjlxMc) {
        this.bzjdrpjlxMc = bzjdrpjlxMc;
    }

    public Integer getBzjdrdrZt() {
        return bzjdrdrZt;
    }

    public void setBzjdrdrZt(Integer bzjdrdrZt) {
        this.bzjdrdrZt = bzjdrdrZt;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public Long getBzjdrId() {
        return bzjdrId;
    }

    public void setBzjdrId(Long bzjdrId) {
        this.bzjdrId = bzjdrId;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public Date getBzjdrSystemDate() {
        return bzjdrSystemDate;
    }

    public void setBzjdrSystemDate(Date bzjdrSystemDate) {
        this.bzjdrSystemDate = bzjdrSystemDate;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public Gson getGson() {
        return gson;
    }

    public void setGson(Gson gson) {
        this.gson = gson;
    }

    public Date getZzrq() {
        return zzrq;
    }

    public void setZzrq(Date zzrq) {
        this.zzrq = zzrq;
    }

    public Sfk getSfk() {
        return sfk;
    }

    public void setSfk(Sfk sfk) {
        this.sfk = sfk;
    }
}
